import { defineStore } from 'pinia'
import { store } from '@/store'
import { getMenuListReq } from '@/api/menu/index'

export const useMenuStore = defineStore('menu', {
  state: () => ({
    menuList: [] as any[]
  }),
  getters: {
    getMenuList(): any[] {
      return this.menuList
    }
  },
  actions: {
    // 通过接口获取全部菜单栏 - getMenuListReq
    async generateAuthMenuList() {
      try {
        const res = await getMenuListReq()

        if (res.data.length < 1) {
          // message.error("抱歉你暂时无权访问系统")
          return Promise.reject()
        }
        this.menuList = res.data;
      } catch (error) {
        ElMessage.error('抱歉，您暂无无权限访问此系统')
        return Promise.reject()
      }
    }
  }
})

// Vue 组件外使用
export function useMenuStoreWithOut() {
  return useMenuStore(store)
}